package com.qixin.activiti;

import com.qixin.activiti.api.IActivitiService;
import com.qixin.modules.eaApplication.entity.EaBusinessApply;
import com.qixin.modules.eaApplication.service.IEaBusinessApplyServiceCopy;
import com.qixin.modules.eaApplication.vo.EaBusinessApplyPage;
import com.qixin.modules.eaBasic.entity.EaTask;
import com.qixin.modules.eaBasic.mapper.EaTaskMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.util.EmptyUtil;
import org.jeecg.modules.system.vo.ActivitiVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author:wly
 * @Date:2021/10/22
 * @Describtion:
 */
@Api(tags="出差申请单结合工作流")
@Slf4j
@RestController
@RequestMapping("/system/eaBusinessApply/activiti")
public class BusinessApplyActivitiController {
    @Autowired
    private IActivitiService iActivitiService;
    @Autowired
    private IEaBusinessApplyServiceCopy eaBusinessApplyService;
    @Autowired
    private EaTaskMapper eaTaskMapper;

    /**
     * 添加出差申请单信息-添加保存
     *
     * @param eaBusinessApplyPage
     * @return Result
     */
    @AutoLog(value = "出差申请单-添加保存")
    @ApiOperation(value="出差申请单-添加保存", notes="出差申请单-添加保存")
    @PostMapping(value = "/add")
    @Transactional
    public Result<?> add(@RequestBody EaBusinessApplyPage eaBusinessApplyPage) {
        EaBusinessApply eaBusinessApply = new EaBusinessApply();
        BeanUtils.copyProperties(eaBusinessApplyPage,eaBusinessApply);
        eaBusinessApply.setSaveType("add");    //保存
        Map<String,Object> result = eaBusinessApplyService.saveMain(eaBusinessApply,eaBusinessApplyPage.getEaBusinessPerDtlList());
        if(result.get("code").equals("0")){  //成功
            //发起工作流
            ActivitiVo activitiVo = new ActivitiVo();
            activitiVo.setId(EmptyUtil.checkNull(result.get("id")));    //单据id
            activitiVo.setType("4");    //单据类型:1差旅费报销单,2借款单,3还款单,4出差申请单,5出差申请变更单,6招待费申请单,7招待费申请变更单,8招待费报销单,9资金支付审批单,10通讯报销单,11电话记录单
            iActivitiService.addActivitiApply(activitiVo,false);

            return Result.OK(result);
        }else{
            return Result.error(result.get("message").toString());
        }
    }

    /**
     * 添加出差申请单信息-添加提交
     *
     * @param eaBusinessApplyPage
     * @return Result
     */
    @AutoLog(value = "出差申请单-添加提交")
    @ApiOperation(value="出差申请单-添加提交", notes="出差申请单-添加提交")
    @PostMapping(value = "/send")
    @Transactional
    public Result<?> send(@RequestBody EaBusinessApplyPage eaBusinessApplyPage) {
        EaBusinessApply eaBusinessApply = new EaBusinessApply();
        BeanUtils.copyProperties(eaBusinessApplyPage,eaBusinessApply);
        eaBusinessApply.setSaveType("send");    //保存
        String id = EmptyUtil.checkNull(eaBusinessApply.getId());
        Map<String,Object> returnMap = new HashMap<String,Object>();
        if(id.equals("")){
            returnMap = eaBusinessApplyService.saveMain(eaBusinessApply,eaBusinessApplyPage.getEaBusinessPerDtlList());
        }else{
            //判断单据是否已经被提交
            EaBusinessApply eaBusinessApply1 = eaBusinessApplyService.getById(id);
            String status = EmptyUtil.checkNull(eaBusinessApply1.getState());   //当状态为1审批中,2已审批时,提示单据状态已变更,请刷新
            if(status.equals("1") || status.equals("2")){
                return Result.error("单据状态已变更,请刷新");
            }

            //eaTask,单据涉及到的表名(如差旅费报销单ea_travel)
            Map<String,Object> taskParams = new HashMap<String,Object>();
            taskParams.put("form_id",eaBusinessApply.getId());
            List<EaTask> eaTaskList  = eaTaskMapper.selectByMap(taskParams);
            EaTask eaTask = new EaTask();
            if(eaTaskList.size() > 0){
                eaTask = eaTaskList.get(0);
            }

            //判断单据是否已经被变更
            /*String taskId = EmptyUtil.checkNull(activitiVo.getTaskId());    //待办任务id
            Map<String,Object> actRuTaskMap = actEaTaskMapper.selectActRuTask(taskId);
            if(actRuTaskMap == null){
                throw new RuntimeException("该待办已被处理,请刷新待办列表");
            }*/

//            String updateTime = EmptyUtil.checkNull(eaBusinessApply.getTime());   //更新时间
//            String msg = iActivitiService.checkFormChange(updateTime,eaTask.getFormType(),eaTask.getFormId());
//            if(!msg.equals("")){
//                return Result.error(msg + "待办列表");
//            }

            returnMap = eaBusinessApplyService.updateMain(eaBusinessApply,eaBusinessApplyPage.getEaBusinessPerDtlList());

            //工作流:审核通过
            ActivitiVo activitiVo = new ActivitiVo();
            activitiVo.setType("4");     //出差申请单
            activitiVo.setId(eaBusinessApply.getId());
            activitiVo.setAuditComment("");
            iActivitiService.pass(activitiVo,eaTask);
        }

        if(returnMap.get("code").equals("-1")){
            return Result.error(EmptyUtil.checkNull(returnMap.get("message")));
        }else{
            if(id.equals("")){
                //发起工作流
                ActivitiVo activitiVo = new ActivitiVo();
                activitiVo.setId(EmptyUtil.checkNull(returnMap.get("id")));    //单据id
                activitiVo.setType("4");    //单据类型:1差旅费报销单,2借款单,3还款单,4出差申请单,5出差申请变更单,6招待费申请单,7招待费申请变更单,8招待费报销单,9资金支付审批单,10通讯报销单,11电话记录单
                iActivitiService.sendActivitiApply(activitiVo,false);
            }

            returnMap.remove("code");
            returnMap.remove("message");
            return Result.OK(returnMap);
        }
    }
}
